using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Ent.Web.Helper
{
    /// <summary>
    /// Class KeyvalueHandler is used for retreiving the value when the key of a connection string is passed 
    /// </summary>
    public class KeyValueHandler
    {
        private ArrayList _kvList;
        public KeyValueHandler(string s_keyValueList): this(s_keyValueList,';')
        {
        }
        /// <summary>
        /// Overloaded Constructor, accepts the entire string a a parameter
        /// </summary>
        /// <param name="s_keyValueList">Key value list</param>
        public KeyValueHandler(string s_keyValueList, char seperator)
        {
            try
            {
                _kvList = new ArrayList();
                string[] keyValueList = s_keyValueList.Split(new char[] { seperator });
                for (int i = 0; i < keyValueList.Length; i++)
                {
                    string s_keyValueSequence = keyValueList.GetValue(i).ToString().Trim();
                    string[] keyValueSequence = s_keyValueSequence.Split(new char[] { '=' });
                    if (keyValueSequence.Length > 1)
                    {
                        string key = keyValueSequence.GetValue(0).ToString().Trim();
                        string value = keyValueSequence.GetValue(1).ToString().Trim();
                        if (!HasKey(key))
                            _kvList.Add(new KeyValue(key, value));
                        else
                            throw new Exception("'" + key + "' term is described twice.", new Exception(""));
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, new Exception("Error occured at KeyValueHandler's constructor"));
            }
        }
        /// <summary>
        /// Returns the count of the key list
        /// </summary>
        public int Length
        {
            get { return _kvList.Count; }
        }
        /// <summary>
        /// Checks if the passed parameter(key) is available in the entire string
        /// </summary>
        /// <param name="key">Key name</param>
        /// <returns>Return true when key is present in the Key value pair list</returns>
        public bool HasKey(string key)
        {
            try
            {
                bool retValue = false;
                foreach (object _kv in _kvList)
                {
                    KeyValue kv = (KeyValue)_kv;
                    if (kv._Key.ToLower() == key.ToLower())
                    {
                        retValue = true;
                        break;
                    }
                }
                return retValue;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, new Exception("Error occured at KeyValueHandler.HasKey"));
            }
        }
        /// <summary>
        /// Returns the value for the key passed
        /// </summary>
        /// <param name="key">Key Name</param>
        /// <returns>Returns the value for given key</returns>
        public string GetValue(string key)
        {
            try
            {
                string retValue = "";
                foreach (object _kv in _kvList)
                {
                    KeyValue kv = (KeyValue)_kv;
                    if (kv._Key.ToLower() == key.ToLower())
                    {
                        retValue = kv._Value;
                        break;
                    }
                }
                return retValue;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, new Exception("Error occured at KeyValueHandler.GetValue"));
            }
        }
        public KeyValue GetKeyValue(int index)
        {
            try
            {
                return (KeyValue)_kvList[index];
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, new Exception("Error occured at KeyValueHandler.GetKeyValue"));
            }
        }
    }

    /// <summary>
    /// Structure of the key value
    /// </summary>
    public struct KeyValue
    {
        public string _Key;
        public string _Value;
        public KeyValue(string key,string value)
        {
            _Key = key;
            _Value = value;
        }
    }
}
